package com.leyou.user.service;

import com.leyou.common.utils.NumberUtils;
import com.leyou.user.mapper.UserMapper;
import com.leyou.user.pojo.User;
import com.leyou.user.utils.CodecUtils;
import net.bytebuddy.dynamic.scaffold.TypeWriter;
import org.apache.commons.lang3.StringUtils;
import org.springframework.amqp.AmqpException;
import org.springframework.amqp.core.AmqpTemplate;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.StringRedisTemplate;
import org.springframework.stereotype.Service;

import java.util.Date;
import java.util.HashMap;
import java.util.Map;
import java.util.concurrent.TimeUnit;

@Service
public class UserService {

    @Autowired
    private StringRedisTemplate redisTemplate;

    @Autowired
    private AmqpTemplate amqpTemplate;

    private static final String LEYOU_USER_PREFIX = "user:verify:";

    @Autowired
    private UserMapper userMapper;

    public Boolean checkUser(String data, Integer type) {

        User record = new User();
        switch (type) {
            case 1: // 说明验证用户名
                record.setUsername(data);
                break;
            case 2: // 验证手机号
                record.setPhone(data);
                break;
            default:
                return null;
        }

        return this.userMapper.selectCount(record) == 0;
    }

    public Boolean sendVerifyCode(String phone) {

        // 判断手机号
        if (StringUtils.isEmpty(phone)) {
            return null;
        }

        try {
            // 生成验证码
            String code = NumberUtils.generateCode(6);

            // 保存本地redis
            this.redisTemplate.opsForValue().set(LEYOU_USER_PREFIX + phone, code, 5, TimeUnit.MINUTES);

            // 发送消息
            Map<String, String> msg = new HashMap<>();
            msg.put("phone", phone);
            msg.put("code", code);
            this.amqpTemplate.convertAndSend("LEYOU.SMS.EXCHANGE", "sms.verify", msg);
            return true;
        } catch (AmqpException e) {
            e.printStackTrace();
        }
        return false;
    }

    public Boolean register(User user, String code) {
        // 查询redis中的验证码
        String cacheCode = this.redisTemplate.opsForValue().get(LEYOU_USER_PREFIX + user.getPhone());
        // 校验验证码
        if (!StringUtils.equals(code, cacheCode)) {
            return false;
        }

        // 生成盐
        String salt = CodecUtils.generateSalt();
        user.setSalt(salt);

        // 对密码加密
        String password = CodecUtils.md5Hex(user.getPassword(), salt);
        user.setPassword(password);

        // 新增用户
        user.setId(null);
        user.setCreated(new Date());
        Boolean b = this.userMapper.insertSelective(user) == 1;

        // 删除redis中的验证码
        if (b){
            this.redisTemplate.delete(LEYOU_USER_PREFIX + user.getPhone());
        }
        return true;
    }

    public User queryUser(String username, String password) {

        // 执行查询
        User record = new User();
        record.setUsername(username);
        User user = this.userMapper.selectOne(record);

        // 判断用户是否存在
        if (user == null){
            return null;
        }

        // 比较密码
        password = CodecUtils.md5Hex(password, user.getSalt());
        if (!StringUtils.equals(password, user.getPassword())) {
            return null;
        }

        return user;
    }
}
